package com.geeke.org.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.org.entity.Company;
import com.geeke.org.service.CompanyService;
import com.geeke.utils.ResultUtil;

/**
 * 公司 Controller
 * @author
 * @version
 */
@RestController
@RequestMapping(value = "/org/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

    // 必须实现的方法
    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        Company entity = companyService.get(id);

        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    // 必须实现的方法 - 分页查询
    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        // 调用 companyService.listPage()
        Page<Company> result = companyService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    // 必须实现的方法 - 查询所有记录
    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<Company> result = companyService.listAll(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    // 有parent_id字段,必须实现的方法 - 获取树形结构数据
    @PostMapping(value = "tree")
    public ResponseEntity<JSONObject> tree(@RequestBody SearchParams searchParams) {
        List<Company> result = companyService.tree(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    // 必须实现的方法 - 保存/更新记录
    @PostMapping(value = "save")
    @RequiresPermissions(value={"company:create","company:update"}, logical= Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody Company company) {
        String id = companyService.save(company).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    // 必须实现的方法 - 删除记录
    @PostMapping(value = "delete")
    @RequiresPermissions("company:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody Company company) {
        int rows = companyService.delete(company);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    // 必须实现的方法 - 批量保存/更新记录
    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("company:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<Company> companys) {
        List<String> ids = companyService.bulkInsert(companys);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    // 必须实现的方法 - 批量更新记录
    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("company:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<Company> companys) {
        List<String> ids = companyService.bulkUpdate(companys);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    // 必须实现的方法 - 批量删除记录
    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("company:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<Company> companys) {
        int rows = companyService.bulkDelete(companys);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    // 根据需求实现的其他方法
    
} 